//
//  PYCatalogueController.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYCatalogueController.h"
#import "PYCatalogue.h"
#import "PYCatalogueManager.h"
#import "PYCatalogueCell.h"
#import "PYTextFileController.h"
#import "PYImageFileController.h"

@interface PYCatalogueController () <UITableViewDelegate, UITableViewDataSource, PYCatalogueCellDelegate>

@property (nonatomic, strong) UITableView *tbView;
@property (nonatomic, strong) NSMutableArray<PYCatalogue *> *dataArray;
@end

@implementation PYCatalogueController

- (BOOL)autoFitSizeForNaviTitle
{
    return YES;
}

+ (instancetype)controllerWithFullPath:(NSString *)fullPath
{
    PYCatalogueController *controller = [PYCatalogueController new];
    NSArray *dataArray = [PYCatalogueManager getCatalogueWithFullPath:fullPath];
    controller.dataArray = [NSMutableArray arrayWithArray:dataArray];
    controller.title = [fullPath lastPathComponent];
    NSLog(@"选择%@", controller.title);
    return controller;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTbView];
}

#pragma mark - tbView
- (UITableView *)tbView
{
    if (!_tbView) {
        _tbView = [UITableView new];
        [_tbView makeDefaultStyle];
        _tbView.delegate = self;
        _tbView.dataSource = self;
    }
    return _tbView;
}

- (void)initTbView
{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PYCatalogue *data = [self.dataArray objectAtIndex:indexPath.row];
    PYCatalogueCell *cell = [PYCatalogueCell cellWithTableView:tableView data:data delegate:self];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PYCatalogue *data = [self.dataArray objectAtIndex:indexPath.row];
    if (data.type == PYFileTypeDirectory) {
        PYCatalogueController *controller = [PYCatalogueController controllerWithFullPath:data.fullPath];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (data.type == PYFileTypePlist) {
        NSString *text = @"";
        NSArray *arr = [NSArray arrayWithContentsOfFile:data.fullPath];
        if (arr) {
            text = [NSString stringWithFormat:@"%@", arr];
        } else {
            NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:data.fullPath];
            if (dict) {
                text = [NSString stringWithFormat:@"%@", dict];
            }
        }
        PYTextFileController *controller = [PYTextFileController controllerWithText:text];
        controller.title = [data.fullPath lastPathComponent];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (data.type == PYFileTypeLog) {
        NSError *error = nil;
        NSString *text = [NSString stringWithContentsOfFile:data.fullPath encoding:NSUTF8StringEncoding error:&error];
        if (error) {
             NSLog(@"error = %@", error);
        }
        PYTextFileController *controller = [PYTextFileController controllerWithText:text];
        controller.title = [data.fullPath lastPathComponent];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (data.type == PYFileTypeImage) {
        UIImage *image = [UIImage imageWithContentsOfFile:data.fullPath];
        PYImageFileController *controller = [PYImageFileController controllerWithImage:image];
        controller.title = [data.fullPath lastPathComponent];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (void)cell:(PYCatalogueCell *)cell deleteCatalogue:(PYCatalogue *)catalogue
{
    if (!catalogue) {
        NSLog(@"删除的路径对象为空");
        return;
    }
    NSInteger index = [self.dataArray indexOfObject:catalogue];
    [self.dataArray removeObjectAtIndex:index];
    [self.tbView deleteRow:index inSection:0 withRowAnimation:UITableViewRowAnimationTop];
}

@end
